<template>
  <div>
    <el-dialog
      :title="status.isAdd ? '管理员添加' : '管理员修改'"
      :visible.sync="status.isshow"
    >
      <el-form :model="form">
        {{ form }}
        <el-form-item label="所属角色" label-width="120px">
          <el-select v-model="form.roleid" placeholder="请选择所属角色">
            <el-option
              :label="item.rolename"
              :value="item.id"
              v-for="item in rolist"
              :key="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="用户名" label-width="120px">
          <el-input v-model="form.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" label-width="120px">
          <el-input v-model="form.password" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="状态" label-width="120px">
          <el-switch
            v-model="form.status"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="status.isshow = false">取 消</el-button>
        <el-button type="primary" v-if="status.isAdd" @click="add"
          >添 加</el-button
        >
        <el-button type="primary" v-else @click="updata">修 改</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  reqrolelist,
  requseradd,
  requserinfo,
  requseredit,
} from "../../../http/api";
import { successAlert, errorAlert } from "../../../utils/alert";
import { mapGetters, mapActions } from "vuex";

export default {
  components: {},
  props: ["status"],
  data() {
    return {
      isshow: true,
      //角色列表
      rolist: [],
      form: {
        roleid: "",
        username: "",
        password: "",
        status: 1,
      },
    };
  },
  methods: {
    ...mapActions(["changeInfo"]),
    //添加角色
    add() {
      requseradd(this.form).then((res) => {
        this.checkeddata().then(() => {
          if (res.data.code == 200) {
            // 1.弹成功
            successAlert(res.data.msg);
            // 2.form的数据清空一下
            this.empty();
            // 3.弹框消失
            this.status.isshow = false;
            // 4.列表需要重新渲染
            this.$emit("init");
          }
        });
      });
    },
    //验空
    checkeddata() {
      return new Promise((reslove) => {
        if (this.form.first_cateid == "") {
          errorAlert("所属角色不能为空");
          return;
        }

        if (this.form.username == "") {
          errorAlert("用户名不能为空");
          return;
        }
        if (this.form.password == "") {
          errorAlert("密码不能为空");
          return;
        }

        reslove();
      });
    },
    //请空页面
    empty() {
      this.form = {
        roleid: "",
        username: "",
        password: "",
        status: 1,
      };
    },
    //修改
    getOne(uid) {
      requserinfo({
        uid: uid,
      }).then((res) => {
        if (res.data.code == 200) {
          this.form = res.data.list;
          this.form.password = "";
        }
      });
    },
    //修改数据
    updata() {
      requseredit(this.form).then((res) => {
        this.checkeddata().then(() => {
          if (res.data.code == 200) {
            // 1.弹成功
            successAlert(res.data.msg);
            //验证一下刚刚修改的管理员是不是当前登录用户，如果是，就要清除用户信息，并退出登录
            if (this.form.uid === this.info.uid) {
              //清除数据
              this.changeInfo({});
              //退出登录
              this.$router.replace("/login");
              return;
            }
            // 2.form的数据清空一下
            this.empty();
            // 3.弹框消失
            this.status.isshow = false;
            // 4.列表需要重新渲染
            this.$emit("init");
          }
        });
      });
    },
    //获取角色列表
    rolelist() {
      reqrolelist().then((res) => {
        if (res.data.code === 200) {
          this.rolist = res.data.list;
        }
      });
    },
  },
  mounted() {
    //获取列表
    this.rolelist();
  },
  computed: { ...mapGetters(["info"]) },
  watch: {},
};
</script>
<style scoped lang="less">
@import "../../../less/index.less";
</style>
